<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <include file="./Public/Tpl/head.html" title="报损管理" description="药品管理系统." keywords="中南民族大学,药品管理系统"/>
</head>
<body>
<include file="./Public/Tpl/nav.html"/>
<div class="container">
  <div class="col-md-2">
    <div class="panel panel-default">
      <div class="panel-heading">功能</div>
      <div class="list-group list-menu">
        <button type="button" class="list-group-item list-menu active" data-to="0">药品报损</button>
        <button type="button" class="list-group-item list-menu" data-to="1" onclick="loadList(size,page)">报损列表</button>
      </div>
    </div>
  </div>
  <div class="col-md-10">
    <div class="panel panel-default func">
      <div class="panel-heading" style="text-align: center">报损表</div>
      <div class="panel-body"></div>
      <form id="breakageForm" class="form-horizontal">
        <div class="col-md-6">
          <div class="form-group">
            <label for="inputPinYinMa" class="col-sm-3 control-label">拼音码</label>
            <div class="col-sm-9" data-toggle="tooltip" data-placement="top" title="输入拼音码自动获取库存和药品信息">
              <input name="pinyinma" type="text" class="form-control" id="inputPinYinMa" placeholder="请输入"
                     autocomplete="off">
              <input name="stock_id" type="hidden" class="form-control" id="inputStockId" placeholder=""
                     autocomplete="off">
            </div>
          </div>
          <div class="form-group">
            <label for="inputAmount" class="col-sm-3 control-label">库存数量</label>
            <div class="col-sm-9">
              <input name="price" type="number" class="form-control" id="inputAmount" disabled placeholder="0.00">
            </div>
          </div>
          <div class="form-group">
            <label for="inputPiHao" class="col-sm-3 control-label">批号</label>
            <div class="col-sm-9">
              <input name="pihao" type="text" class="form-control" disabled id="inputPiHao" placeholder="0">
            </div>
          </div>
          <div class="form-group">
            <label for="inputPiZhunWenHao" class="col-sm-3 control-label">批准文号</label>
            <div class="col-sm-9">
              <input name="pizhunwenhao" type="text" class="form-control" disabled id="inputPiZhunWenHao"
                     placeholder="0.00">
            </div>
          </div>
          <div class="form-group">
            <label for="inputSellPrice" class="col-sm-3 control-label">销售价</label>
            <div class="col-sm-9">
              <input name="sell_price" type="number" class="form-control" disabled id="inputSellPrice" placeholder="">
            </div>
          </div>
          <div class="form-group">
            <label for="inputBreakAmount" class="col-sm-3 control-label">报损数量</label>
            <div class="col-sm-9">
              <input name="break_amount" type="number" class="form-control" id="inputBreakAmount" placeholder="未填写">
            </div>
          </div>
          <div class="form-group">
            <label for="inputBreakPrice" class="col-sm-3 control-label">报损总额</label>
            <div class="col-sm-9">
              <input name="allprice" type="number" class="form-control" id="inputBreakPrice" placeholder="未填写">
            </div>
          </div>

        </div>
        <div class="col-md-6">
          <div class="form-group">
            <label for="inputName" class="col-sm-3 control-label">名称</label>
            <div class="col-sm-9">
              <input name="name" type="text" class="form-control" readonly id="inputName" placeholder="药品名称">
            </div>
          </div>
          <div class="form-group">
            <label for="inputSpec" class="col-sm-3 control-label">规格大小</label>
            <div class="col-sm-9">
              <input name="spec" type="text" class="form-control" disabled id="inputSpec" placeholder="规格">
            </div>
          </div>
          <div class="form-group">
            <label for="inputUnit" class="col-sm-3 control-label">计量单位</label>
            <div class="col-sm-9">
              <input name="unit" type="text" class="form-control" disabled id="inputUnit" placeholder="单位">
            </div>
          </div>
          <div class="form-group">
            <label for="inputFactory" class="col-sm-3 control-label">生产厂家</label>
            <div class="col-sm-9">
              <input name="factory" type="text" class="form-control" disabled id="inputFactory" placeholder="厂家">
            </div>
          </div>
          <div class="form-group">
            <label for="inputProduceDate" class="col-sm-3 control-label">生产日期</label>
            <div class="col-sm-9">
              <input name="producedate" type="text" class="form-control" id="inputProduceDate" disabled placeholder="">
            </div>
          </div>
          <div class="form-group">
            <label for="inputUsefulDate" class="col-sm-3 control-label">失效日期</label>
            <div class="col-sm-9">
              <input name="usefuldate" type="text" class="form-control" id="inputUsefulDate" disabled placeholder="">
            </div>
          </div>
          <div class="form-group">
            <label for="inputReason" class="col-sm-3 control-label">原因</label>
            <div class="col-sm-9">
              <input name="reason" type="text" class="form-control" id="inputReason" placeholder="未填写">
            </div>
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-9">
            <button id="doBreak" type="button" class="btn btn-danger submit">报损</button>
            <button type="reset" class="btn btn-success">清空</button>
          </div>
        </div>
      </form>
    </div>
    <div class="panel panel-default func" id="breakageList">
      <div class="panel-heading" style="text-align: center">报损列表</div>
      <table class="table table-hover">
        <thead>
        <tr>
          <th>报损编号</th>
          <th>库存编号</th>
          <th>药品名称</th>
          <th>规格</th>
          <th>单位</th>
          <th>单价</th>
          <th>报损数量</th>
          <th>总额</th>
          <th>报损日期</th>
          <th>操作员</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="info in list">
          <td>{{ info.break_id }}</td>
          <td>{{ info.stock_id }}</td>
          <td>{{ info.name }}</td>
          <td>{{ info.spec }}</td>
          <td>{{ info.unit }}</td>
          <td>{{ info.sellprice }}</td>
          <td>{{ info.break_amount }}</td>
          <td>{{ info.allprice }}</td>
          <td>{{ info.time }}</td>
          <td>{{ info.realname }}</td>
        </tr>
        </tbody>
      </table>
      <div id="breakagePage" class="center">
        <nav>
          <ul class="pagination">
            <li v-for="i in count">
              <a href="javascript:" :data-to="i">{{ i }}</a>
            </li>
          </ul>
        </nav>
      </div>
    </div>
  </div>
</div>
<include file="./Public/Tpl/footer.html"/>
<js href="__PUBLIC__/js/jquery.Kcomplete-1.1.min.js"/>
<script>
    var size = 10;
    var page = 1;
    var breakageList = {list: [], count: 10};
    var app = new Vue({
        el: '#breakageList',
        data: breakageList
    });
    function loadList(size, page) {
        $.ajax({
            type: 'POST',
            url: "{:U('Breakage/getBreakageList')}",
            data: {"size": size, "page": page},
            dataType: 'json',
            async:false,
            success: function (rs) {
                if (rs.code == 200) {
                    breakageList.list = rs.data.list;
                    breakageList.count = Math.ceil(rs.data.count / size);
                }
            },
            beforeSend: function () {
                this.layerIndex = layer.load(1, {shade: [0.1, '#393D49']});
            },
            complete: function () {
                layer.close(this.layerIndex);
            }
        });
    }
    $(document).ready(function () {
        //加载更多
        layui.use(['laypage', 'layer'], function () {
            loadList(size,page);
            layer = layui.layer;
            laypage = layui.laypage;
            laypage({
                cont: 'breakagePage'
                , pages: breakageList.count
                , skin: '#337ab7'
                , skip: true,
                jump: function (obj, first) {
                    if (!first) {
                        page = obj.curr;
                        loadList(size, page);
                    }
                }
            });
        });
        //退货按钮
        $("#doBreak").on("click", function () {
            var amount = $("#inputBreakAmount").val();
            var price = $("#inputBreakPrice").val();
            if (amount == 0 || price == 0) {
                layer.msg("数量和价格不能为空或为零!",{icon:2});
                return;
            }
            var Form = $("#breakageForm");
            $.ajax({
                url: "{:U('Breakage/doAdd')}",
                data: Form.serialize(),
                dataType: "json",
                type: "POST",
                success: function (data) {
                    if (data['code'] == 200) {
                        layer.msg("成功确认报损!",{icon:1});
                        Form[0].reset();
                    } else {
                        layer.msg(data['msg'],{icon:2});
                    }
                },
                error: function () {
                    layer.msg("提交过程中发生问题! ",{icon:0});
                },
                beforeSend:function(){
                    this.layerIndex = layer.load(0,{ shade: [0.1, '#393D49'] });
                },
                complete:function(){
                    layer.close(this.layerIndex);
                }
            })
        });
        //填写完数量
        $("#inputBreakAmount").on("blur", function () {
            $("#inputBreakPrice").attr("value", this.value * $("#inputSellPrice").val());
        });
        //拼音码自动提示
        $("#inputPinYinMa").Kcomplete({
            location: "{:U('Drug/nameTips')}",
            dataType: 'json'
        });
        //销售单事件
        $("#inputPinYinMa").bind("keyup change", function () {
            $.ajax({
                url: "{:U('Stock/getInfo')}",
                data: "pinyinma=" + $("#inputPinYinMa").val(),
                datatype: "json",
                type: "POST",
                success: function (data) {
                    console.log(data);
                    if (data['code'] == 200) {
                        var tempName = data.data.name;
                        var tempSpec = data.data.spec;
                        var tempUnit = data.data.unit;
                        var tempFactory = data.data.factory;
                        var tempProduceDate = formatDate(data.data.producedate);
                        var tempUsefulDate = formatDate(data.data.usefuldate);
                        var tempPrice = data.data.sellprice;
                        var tempAmount = data.data.stock_amount;
                        var tempPiHao = data.data.pihao;
                        var tempPiZhunWenHao = data.data.pizhunwenhao;
                        var tempStockID = data.data.stock_id;
                    } else {
                        var tempName = data.msg;
                        var tempSpec = data.msg;
                        var tempUnit = data.msg;
                        var tempFactory = data.msg;
                        var tempProduceDate = data.msg;
                        var tempUsefulDate = data.msg;
                        var tempPrice = data.msg;
                        var tempAmount = data.msg;
                        var tempPiHao = data.msg;
                        var tempPiZhunWenHao = data.msg;
                    }
                    $("#inputName").attr("value", tempName);
                    $("#inputSpec").attr("value", tempSpec);
                    $("#inputUnit").attr("value", tempUnit);
                    $("#inputFactory").attr("value", tempFactory);
                    $("#inputProduceDate").attr("value", tempProduceDate);
                    $("#inputUsefulDate").attr("value", tempUsefulDate);
                    $("#inputSellPrice").attr("value", tempPrice);
                    $("#inputAmount").attr("value", tempAmount);
                    $("#inputPiHao").attr("value", tempPiHao);
                    $("#inputPiZhunWenHao").attr("value", tempPiZhunWenHao);
                    $("#inputStockId").attr("value", tempStockID);
                },
                error: function () {
                },
            })
        });
    });
</script>
</body>
</html>